package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author spt
 * @description 预约设置控制器
 * @date
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) throws IOException {
        log.info("上传文件...");
        //调用业务层进行处理
        orderSettingService.saveOrderSetting(excelFile);
        return new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据id查询预约信息,包括套餐信息和会员信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map<String,Object> map=orderSettingService.findById(id);
            //查询预约信息成功
            return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            //查询预约信息失败
            return new Result(false,MessageConst.QUERY_ORDER_FAIL);
        }
    }


    /**
     * 展示预约信息
     * @param
     * @return
     */
     // 根据月份查询对应的预约设置数据
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(Integer year,Integer month){
        try {
                String date = year+"-"+month;
            //  查询出来的是多条 用list 集合
            //   是因为前端的返回值不满足要求   根据页面所需要的结构去封装map对象    date是年月
            List<Map> list= orderSettingService.getOrderSettingByMonth(date);
            return  new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }
    /**
     *  根据日期设置对应的预约设置数据
     * @param orderSetting
     * @return
     */
     @RequestMapping("/editNumberByDate")
       //  因为是json格式的要加@ResqueatBody 直接使用OrderSetting的实体类
     public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
         try {
            orderSettingService.editNumberByDate(orderSetting);
             return  new Result(true,MessageConst.ORDERSETTING_SUCCESS);
         } catch (Exception e) {
             e.printStackTrace();
             return  new Result(false,MessageConst.ORDERSETTING_FAIL);
         }
     }

}
